package cn.edu.sjtu.process_management.exception.handler;

import cn.edu.sjtu.process_management.exception.model.*;
import cn.edu.sjtu.process_management.constant.RespStatus;
import cn.edu.sjtu.process_management.model.Result;
import cn.edu.sjtu.process_management.util.ResultUtil;
import com.mongodb.MongoException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author loumoon
 * @date 2019-11-07 13:29
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    private final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(MongoException.class)
    public Result handleMongoException(final MongoException exception) {
        log.warn("Processing MongoException: {}", exception.getMessage());
        return ResultUtil.error(RespStatus.SERVER_ERROR);
    }

    @ExceptionHandler(ServiceException.class)
    public Result handleServiceException(final ServiceException exception) {
        log.warn("Processing ServiceException at " + exception.getService() + ": {}", exception.getStatus().getMessage());
        return ResultUtil.error(exception.getStatus());
    }

    @ExceptionHandler(FileException.class)
    public Result handleFileException(final FileException exception) {
        log.warn("Processing FileException: {}", exception.getStatus().getMessage());
        return ResultUtil.error(exception.getStatus());
    }

    @ExceptionHandler(RequestException.class)
    public Result handleRequestException(final RequestException exception) {
        log.warn("Processing RequestException: {}", exception.getStatus().getMessage());
        return ResultUtil.error(exception.getStatus());
    }

    @ExceptionHandler(BpmnException.class)
    public Result handleBpmnException(final BpmnException exception) {
        log.warn("Processing BpmnException: {}", exception.getStatus().getMessage());
        return ResultUtil.error(exception.getStatus());
    }

    @ExceptionHandler(OperatorException.class)
    public Result handleOperatorException(final OperatorException exception) {
        log.warn("OperatorSet Exception: {}", exception.getStatus().getMessage());
        return ResultUtil.error(exception.getStatus());
    }
}
